<template>
    <el-form :model="loginForm" ref="loginRef" :rules="rules"  class="login">
      <h1>通用后台管理系统</h1>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名" clearable />
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" show-password placeholder="请输入密码" clearable />
      </el-form-item>
      <el-form-item prop="code">
        <el-space wrap >
          <el-input v-model="loginForm.code"   placeholder="验证码"/>
          <div @click="handleCaptcha" style="margin-top: 8px;">
            <el-image :src="imagesBase64"/>
          </div>
         
        </el-space>
      </el-form-item>
      <div>
        <el-checkbox v-model="loginForm.rememberMe" label="记住我" size="large" />
        <div class="login-btn">
          <el-button type="primary" @click="handleSubmit(loginRef)" style="width: 180px;">登录</el-button>
          <el-button type="primary" @click="handleReset(loginRef)" style="width: 180px;">重置</el-button>
        </div>
      </div>
    </el-form>

</template>

<script setup>
import { ref,onMounted } from "vue";
import user from '@/services/user'
import login from '@/services/login'
import { useRouter } from 'vue-router'
import { ElMessage } from "element-plus";
import { setLoginInfo,removeLoginInfo,getLoginInfo } from "@/utils/logininfo";

const imagesBase64 = ref()
const init = async () => {
    const res = await login.generateCaptcha()
    if(res.code === 200) {
      imagesBase64.value = res.data
    }
}
onMounted(()=>{
  init()
})

const router = useRouter()
const loginForm = ref({
  username: null,
  password: null,
  code: '',
  rememberMe: false
});

const loginRef = ref();

//获取记住密码的数据
const rememberInfo = getLoginInfo();
if(rememberInfo){
    //将账号密码设置到页面，并勾选多选框
    loginForm.value.username = rememberInfo.username;
    loginForm.value.password = rememberInfo.password;
    loginForm.value.rememberMe = true;
}

const handleSubmit = (loginRef) => {
  if (!loginRef) return;
  loginRef.validate(async (valid) => {
    if (valid) {
      const res = await user.login(loginForm.value)
      if (res.code === 200) {
        
        ElMessage.success('登录成功')

        //勾选了记住密码
        if (loginForm.value.rememberMe) {
          setLoginInfo({
            username: loginForm.value.username,
            password: loginForm.value.password,
          });
        }else {
          removeLoginInfo();
        }
      } else {
        ElMessage.error(res.msg)
        handleCaptcha()
      }
      router.push("/home")
    } else {
      console.log("error submit!");
    }
  });
};

const rules = ref({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});


// 重置密码
const handleReset = (formRef) => {
  loginForm.value = {
    username: '',
    password: ''
  }
  formRef.resetFields()
}

// 生成验证码
const handleCaptcha = async ()=>{
  const res = await login.generateCaptcha()
    if(res.code === 200) {
      imagesBase64.value = res.data
    }
}

// const handleRememberMe = () => {
//    = 
// }
</script>

<style lang="less" scoped>
.login {
  width: 300px;
  margin: 0 auto;
  margin-top: 250px;

  h1 {
    text-align: center;
  }

  .login-btn {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
  }
}

</style>
